#include "GameCore.h"

using namespace sleek;
using namespace sleek::core;

namespace interne
{
    GameCore::GameCore()
    {
        device::Device_stub info = device::Device_stub(1024,640,32,false);
//        screen = CreateDeviceWindowManager(device::DWM_X11, info);
        screen = CreateDeviceWindowManager(device::DWM_SDL, info);

        sleek::core::math::vector2du wsize = (screen->getDesktopVideoSize()-screen->getInfo().size)/2;
        screen->setWindowPos(sleek::core::math::vector2di(wsize.width, wsize.height));
        screen->enableAntialiasing(device::DAM_FAST);
        screen->WriteConfig("data/init");
        screen->setCaption("SleekThink !");

        everythink = new engine::world(screen);
        everythink->getInitError().print();
//        everythink->enableThread(engine::etf_event | engine::etf_sound);

        everythink->getInterface()->getCursor()->enablePhysics();
        everythink->getInterface()->getCursor()->showCursor(false);
        everythink->getInterface()->getCursor()->showTexture(true);
        everythink->getInterface()->getCursor()->setTexture(texture::LoadTextureFromBmp("element/pointor.bmp"));

        fps = everythink->getInterface()->addStaticText("FPS:", core::math::aabbox2di(core::math::vector2di(0,1),core::math::vector2di(100,40)));
        gui::font *tmp = new gui::font("ttf/Prototype.ttf");
        tmp->setColor(texture::pixel(255,64,0));
        fps->setFont(tmp);
        everythink->getInterface()->addCustomFrame(fps);

        phy = new Physic(this);
        edit = new GameEditor(this, everythink);
    }
    GameCore::~GameCore()
    {
        screen->setEventReceiver(0);
        if(everythink) delete everythink;
        if(screen) delete screen;
        delete phy;
    }
    void GameCore::run()
    {

        int i = 0;
        srand(clock());
        while(screen->run())
        {
            fps->setText(core::os::stringc("FPS: ") + everythink->getDevice()->getFPS());
            phy->step();
            screen->begin(texture::pixel(25,50,50));
                glEnable(GL_STENCIL_TEST);
                    edit->render();
                    everythink->render();
                glDisable(GL_STENCIL_TEST);
                phy->debug();
            screen->end();
        }
    }
}
